export default {
	//获取用户信息
	getUserInfo(successFn) {
		let token = uni.getStorageSync("token");
		let uid = uni.getStorageSync("uid");
		let userInfoGo = null; //用户信息实体
		let params = {};
		params.uid = uid;
		uniCloud
			.callFunction({
				name: "user-center",
				data: {
					action: "getUserInfo",
					uniIdToken: token,
					params,
				},
			})
			.then((res) => {
				// debugger
				console.log(res);
				if (res.result.code != 0) throw new Error(res.result.msg);
				userInfoGo = res.result.userInfo;
				// 如果存在小程序信息，请求小程序用户表返回对应数据
				let ali_openid = userInfoGo.ali_openid
				let hasWxOpenId = userInfoGo.wx_openid && userInfoGo.wx_openid["mp-weixin"] //存在
				let mp_wx_openid = hasWxOpenId ? userInfoGo.wx_openid["mp-weixin"] : '';
				let openId = ali_openid || mp_wx_openid
				if (openId) {
					return uniCloud.callFunction({
						name: "database",
						data: {
							collection: "mpuser",
							type: "doc",
							docId: openId,
						},
					});
				} else {
					return new Promise((resolve, reject) => {
						resolve("没有小程序信息");
					});
				}
			})
			.then((res) => {
				console.log("query-mpuser-data", res);
				let mpUserInfo = null;
				if (res.result && res.result.data.length > 0)
					mpUserInfo = res.result.data[0];
				let ali_openid = userInfoGo.ali_openid //是否存在支付宝小程序用户信息
				if (ali_openid) {
					userInfoGo.aliUserInfo = mpUserInfo; //赋值小程序用户信息
				} else {
					userInfoGo.wxUserInfo = mpUserInfo; //赋值小程序用户信息
				}
				console.log("userInfoGo", userInfoGo);
				uni.setStorageSync("userInfo", userInfoGo);
				if (successFn) successFn();
			})
			.catch((err) => {
				console.error(err);
				toast(err.message);
			});
	}, //修改用户头像
	setUserAvatar(successFn) {
		uni.chooseImage({
				count: 1,
			}).then((res) => {
				// debugger;
				console.log(res);
				res = res[1];
				if (res.tempFilePaths.length > 0) {
					showLoading()
					let filePath = res.tempFilePaths[0];
					let timestamp = Date.parse(new Date());
					//进行上传操作 -promise方式
					return uniCloud.uploadFile({
						filePath: filePath,
						cloudPath: `avatar-${timestamp}.jpg`,
					});
				}
			})
			.then((result) => {
				console.log(result);
				let fileUrl = result.fileID;
				let uid = uni.getStorageSync("uid");
				let token = uni.getStorageSync("token");
				return uniCloud.callFunction({
					name: "user-center",
					data: {
						action: "setAvatar",
						uniIdToken: token,
						params: {
							uid: uid,
							avatar: fileUrl,
						},
					},
				});
			})
			.then((res) => {
				toast("修改成功!");
				this.getUserInfo(successFn)
			})
			.catch((err) => {
				console.error("setUserAvatar", err.message);
				toast("修改失败!");
			}).finally(() => {
				hideLoading()
			})
	}, // 更新用户信息
	updateUserInfo(params, successFn) {
		showLoading()
		let uid = uni.getStorageSync("uid");
		let token = uni.getStorageSync("token");
		params.uid = uid;
		uniCloud.callFunction({
				name: "user-center",
				data: {
					action: "updateUser",
					uniIdToken: token,
					params,
				},
			})
			.then((res) => {
				// debugger
				toast("修改成功!");
				this.getUserInfo(successFn)
			})
			.catch((err) => {
				toast("修改失败!");
			}).finally(() => {
				hideLoading()
			})
	},
};

function toast(content) {
	uni.showToast({
		title: content,
		icon: "none",
	});
}

function showLoading() {
	uni.showLoading({
		title: "提交中...",
		mask: true
	})
}

function hideLoading() {
	uni.hideLoading()
}
